%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Scientific Visualization and Virtual Reality
%       Wouter Josemans and Ninghang Hu
%               October 28, 2010
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Flickr Amsterdam GUI
function varargout = FlickrGUI(varargin)
% FLICKRGUI M-file for FlickrGUI.fig
%      FLICKRGUI, by itself, creates a new FLICKRGUI or raises the existing
%      singleton*.
%
%      H = FLICKRGUI returns the handle to a new FLICKRGUI or the handle to
%      the existing singleton*.
%
%      FLICKRGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in FLICKRGUI.M with the given input arguments.
%
%      FLICKRGUI('Property','Value',...) creates a new FLICKRGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before FlickrGUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to FlickrGUI_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help FlickrGUI

% Last Modified by GUIDE v2.5 22-Oct-2010 14:29:36

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @FlickrGUI_OpeningFcn, ...
                   'gui_OutputFcn',  @FlickrGUI_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before FlickrGUI is made visible.
function FlickrGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to FlickrGUI (see VARARGIN)

% Choose default command line output for FlickrGUI
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);
% UIWAIT makes FlickrGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);


load FlickrAmsterdam
    
    % remove duplicates
%     [uId,m,n] = unique(photodata(:,1));
%     uPos = pos(m,:);
global data;

% --- Outputs from this function are returned to the command line.
function varargout = FlickrGUI_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

    global data;
    initCityMap(handles);
    MAX_DATE = 734428.532;
    MIN_DATE = 732315.000;
    
    % get slider status
    Max = get(hObject,'Max');
    Min = get(hObject,'Min');
    Pos = get(hObject,'Value');
    
    NumOfWeeks = floor((MAX_DATE - MIN_DATE) / 7);
    Start_Date = MIN_DATE - 1 + floor((Pos - Min) * NumOfWeeks * 7 /(Max - Min));
    End_Date = Start_Date + 6;
    
    axes(handles.axes1);
    cla;
    lim = [0,4];
    
    [heatmap,numberOfPhotos] = generateMap(Start_Date,End_Date,lim,data);
    
    setInfo(Start_Date,End_Date,numberOfPhotos,handles);
    showHeatMap(handles,heatmap);
    
    
    
    
    
% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on button press in animate.
function animate_Callback(hObject, eventdata, handles)
% hObject    handle to animate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    global data;
    initCityMap(handles);
    MAX_DATE = 734428.532;
    MIN_DATE = 732315.000;
    Start_Date = MIN_DATE;
    set(handles.animate,'UserData',1);
    
    if get(handles.demoByWeek,'value') == 1
        while (Start_Date + 6 <= MAX_DATE && get(handles.animate,'UserData') ==1)
            if get(handles.demoByWeek,'value') == 0
                break
            end
            End_Date = Start_Date + 6;
            axes(handles.axes1);
            cla;
            lim = [0,4];
            [heatmap,numberOfPhotos] = generateMap(Start_Date,End_Date,lim,data);

            showHeatMap(handles,heatmap);
            setInfo(Start_Date,End_Date,numberOfPhotos,handles)
            pause(1)

            Start_Date = Start_Date + 7;

        end
    else
        start_day = datenum('01-jan-2005');
        for year = 2005:2010
            for month = 1:12
                if get(handles.demoByWeek,'value') == 1 || get(handles.animate,'UserData') == 0
                    break
                end
                end_day = start_day + daysinmonth(year,month) - 1;
                axes(handles.axes1);
                cla;
                lim = [0,20];
                [heatmap,numberOfPhotos] = generateMap(start_day,end_day,lim,data);

                showHeatMap(handles,heatmap);
                setInfo(start_day,end_day,numberOfPhotos,handles)
                pause(1)
                
                p = (start_day - MIN_DATE)/(MAX_DATE - MIN_DATE);
                
                if p < 0
                    p = 0;
                end
                Max = get(handles.slider1,'Max');
                Min = get(handles.slider1,'Min');
                set(handles.slider1,'Value',(Max-Min)*p+Min);
                pause(1)
                start_day = end_day + 1;
            end
        end 
    end
    guidata(hObject, handles);


% --- Executes on button press in stop_animate.
function stop_animate_Callback(hObject, eventdata, handles)
% hObject    handle to stop_animate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    set(handles.animate,'UserData',0);
    guidata(hObject,handles);


function Customized_Start_Date_Callback(hObject, eventdata, handles)
% hObject    handle to Customized_Start_Date (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
start_date = datenum(get(hObject, 'String'));
handles.metricdata.start_date = start_date;
guidata(hObject,handles)
% Hints: get(hObject,'String') returns contents of Customized_Start_Date as text
%        str2double(get(hObject,'String')) returns contents of Customized_Start_Date as a double


% --- Executes during object creation, after setting all properties.
function Customized_Start_Date_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Customized_Start_Date (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function Customized_End_Date_Callback(hObject, eventdata, handles)
% hObject    handle to Customized_End_Date (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
end_date = datenum(get(hObject, 'String'));
handles.metricdata.end_date = end_date;
guidata(hObject,handles)
% Hints: get(hObject,'String') returns contents of Customized_End_Date as text
%        str2double(get(hObject,'String')) returns contents of Customized_End_Date as a double


% --- Executes during object creation, after setting all properties.
function Customized_End_Date_CreateFcn(hObject, eventdata, handles)

% hObject    handle to Customized_End_Date (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in FindSpecificDate.
function FindSpecificDate_Callback(hObject, eventdata, handles)
% hObject    handle to FindSpecificDate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    global data;
    initCityMap(handles);
    Start_Date = handles.metricdata.start_date;
    End_Date = handles.metricdata.end_date;
    axes(handles.axes1);
    cla;
    
    [heatmap,numberOfPhotos] = generateMap(Start_Date,End_Date,false,data);
    
    showHeatMap(handles,heatmap);
    setInfo(Start_Date,End_Date,numberOfPhotos,handles)
    
function initCityMap(handles)
    axes(handles.citymap);
    cla
    imshow(imread('citymap100x100.jpg'))
    drawnow;
    
function showHeatMap(handles,heatmap)
    axes(handles.heatmap)
    cla
    imshow(imread('citymap100x100.jpg'))
    hold on;
    imghandler = imshow(flipdim(imrotate(heatmap,180),2));
    hold off;
    set(imghandler,'alphadata',0.5) 
    colormap('jet')
    drawnow;
    
function setInfo(Start_Date,End_Date,numberOfPhotos,handles)
    set(handles.text_start_date,'string',datestr(Start_Date,1));
    set(handles.text_end_date,'string',datestr(End_Date,1));
    set(handles.numberOfPhotos,'string',numberOfPhotos);


% --- Executes on button press in demoByWeek.
function demoByWeek_Callback(hObject, eventdata, handles)
% hObject    handle to demoByWeek (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%     if get(handles.demoByMonth,'')
%     set(handles.demoByWeek,'userdata',1);
% Hint: get(hObject,'Value') returns toggle state of demoByWeek


% --- Executes on button press in demoByMonth.
function demoByMonth_Callback(hObject, eventdata, handles)
% hObject    handle to demoByMonth (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%     set(handles.demoByWeek,'userdata',0);
% Hint: get(hObject,'Value') returns toggle state of demoByMonth
